<?php

namespace app\console\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;

class ImportSnackShort extends Command
{
    protected function configure()
    {
        $this->setName('import_snack_short')
            ->setDescription('import_snack_short video files');
    }

    protected function execute(Input $input, Output $output)
    {
        $output->info("execute import_snack_short");

//        $bookName = 'Never Enjoyed Divorce Like This';
//        $bookList = Db::name('snackshow_book')->where('book_name', $bookName)->select();

        $bookList = Db::name('snackshow_book')->select();
        foreach ($bookList as $bookInfo) {
            $bookNo = $bookInfo['book_no'];
            $dirPath = '/uploads/20240907/' . strtoupper(md5($bookNo));

            $bookName = $bookInfo['book_name'];

            echo '$bookName = ' . $bookName . PHP_EOL;
            $videoListInfo = Db::name('videolist')->where('name', $bookName)->find();
            if (empty($videoListInfo)) {
                $videoListInfo = null;
                $videoListInfo['name'] = $bookInfo['book_name'];
                $videoListInfo['img'] = $bookInfo['cover_image'];
                $videoListInfo['updatetime'] = time();
                $videoListInfo['createtime'] = time();
                $videoListInfo['type'] = $bookInfo['category'];
                $videoListInfo['status'] = 'hidden';
                $videoListInfo['text'] = $bookInfo['tags'];
                $videoListInfo['info'] = $bookInfo['introduce'];
                $videoListInfo['story'] = substr($bookInfo['introduce'], 0, 256);
                $videoListInfo['uid'] = 1;
                $videoListInfo['lx'] = 1;

                try {
                    $videoListId = Db::name('videolist')->insert($videoListInfo, false, true);
                } catch (\Exception $exception) {
                    var_dump($videoListInfo);
                    var_dump($exception->getMessage());
                    exit();
                    continue;
                }
            } else {
                $videoListId = $videoListInfo['id'];
            }

            // 循环更新免费剧集URL
//            $chapterList = Db::name('snackshow_chapter')->where('book_no', $bookInfo['book_no'])->where('chapter_price', 0)->select();
            $chapterList = Db::name('snackshow_chapter')->where('book_no', $bookInfo['book_no'])->select();
            foreach ($chapterList as $chapterItem) {

                $videoInfo = Db::name('video')->where('pid', $videoListId)->where('weigh', $chapterItem['chapter_order'])->find();
                if (empty($videoInfo)) {
                    $videoInfo = null;
                    $videoInfo['name'] = $chapterItem['chapter_name'];
                    $videoInfo['updatetime'] = time();
                    $videoInfo['createtime'] = time();
                    $videoInfo['videourl'] = $dirPath . '/' . str_pad($chapterItem['chapter_order'], 2, 0, STR_PAD_LEFT) . '.mp4';
                    $videoInfo['img'] = $bookInfo['cover_image'];
                    $videoInfo['status'] = 'normal';
                    $videoInfo['pid'] = $videoListId;
                    $videoInfo['uid'] = 1;
                    $videoInfo['lx'] = 1;
                    $videoInfo['weigh'] = $chapterItem['chapter_order'];
                    $videoId = Db::name('video')->insert($videoInfo, false, true);
                }

            }
        }
    }
}
